package NC5_二叉树根节点到叶子节点的所有路径和;

import java.util.*;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
}

public class Solution {
    /**
     *
     * @param root TreeNode类
     * @return int整型
     */
    public int sumNumbers (TreeNode root) {
        // write code here
        int sum = 0;
        if (root == null){
            return sum;
        }

        return preorderSumNumbers(root, sum);
    }

    private int preorderSumNumbers(TreeNode root, int sum) {
        if (root == null){
            return 0;
        }
        sum = sum * 10 + root.val;
        if (root.left == null && root.right == null){
            return sum;
        }
        return preorderSumNumbers(root.left, sum) + preorderSumNumbers(root.right, sum);
    }
}